<?php

/**
 * Description of config
 * @author clive
 */
class model_firadio_php_sys_config extends db {

    public function __construct() {
        $db = db::getInstance();
        $this->db1 = $db->table("firphp_configclass");
        $db = db::getInstance();
        $this->db2 = $db->table("firphp_configitem");
        $db = db::getInstance();
        $this->db3 = $db->table("firphp_configarray");
    }

    public function outconfigfile($array) {
        $data = '<?php' . "\r\n";
        $data.='return(';
        $data.=var_export($array, true);
        $data.=');';
        echo $data;
        file_put_contents(app::$APP_ROOTDIR . __DS__ . 'config' . __DS__ . 'root.php', $data);
    }

    public function getlist() {
        $rows = $this->db1->orderby('level ASC')->select();
        $item = $items = array();
        foreach ($rows as $row) {
            $item[$row['id']] = array();
            if ($row['isarray'] != 0) {
                $item[$row['id']] = NULL;
            }
            if ($row['level'] == 0) {
                $items[$row['name']] = &$item[$row['id']]; //采用地址引用方式传递
            } else {
                $item[$row['parentid']][$row['name']] = &$item[$row['id']];
            }
        }
        $rows = $this->db2->orderby('id ASC')->select();
        foreach ($rows as $row) {
            //1:勾选,2:单选,3:列表,4:整数,5:小数,6:单行文本,7:多行文本,8:日期选择,9:日期时间
            if (is_array($item[$row['configclassid']])) {
                $value = NULL;
                switch ($row['type']) {
                    case(1):case(4)://整数
                        $value = intval($row['value_int']);
                        break;
                    case(5)://小数
                        $value = doubleval($row['value_double']);
                        break;
                    case(2):case(3):case(6)://字符串
                        $value = trim($row['value_char']);
                        break;
                    case(7)://多行文本框输入
                        $value = trim($row['value_text']);
                        break;
                }
                $item[$row['configclassid']][$row['name']] = $value;
            }
        }
        $rows = $this->db3->orderby('id ASC')->select();
        foreach ($rows as $row) {
            $item[$row['configclassid']][$row['name']] = json_decode($row['value_json'], true);
        }
        return($items);
    }

}

